/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.cell;

import com.google.gwt.resources.client.ImageResource;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.event.SelectEvent;


/**
 * A cell with only an icon on which you can click
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <T>
 */
public abstract class ListStoreIconButtonCell<T> extends IconButtonCell
{

	private final ListStore<T> listStore;

	/**
	 * A clickable icon without tooltip
	 * 
	 * @param icon
	 *           the icon
	 * @param listStore
	 *           the associated store to be able to retrieve object on which the user clicked
	 */
	public ListStoreIconButtonCell(final ImageResource icon, final ListStore<T> listStore)
	{
		this(icon, null, listStore);
	}

	/**
	 * A clickable ico
	 * 
	 * @param icon
	 *           the icon
	 * @param tooltip
	 *           the tooltip to display on icon (can be null if you on't want to display a tooltip)
	 * @param listStore
	 *           the associated store to be able to retrieve object on which the user clicked
	 */
	public ListStoreIconButtonCell(final ImageResource icon, final String tooltip, final ListStore<T> listStore)
	{
		super(icon);
		this.listStore = listStore;
		setTooltip(tooltip);
		addSelectHandler(new SelectEvent.SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				ListStoreIconButtonCell.this.onClick(listStore.findModelWithKey(event.getContext().getKey().toString()), event
						.getContext().getColumn(), event.getContext().getIndex(), event.getContext().getKey().toString());
			}
		});
		getAppearance().setCellIconVisibilityHandler(new CellIconVisiblityHandler()
		{
			@Override
			public boolean isCellIconVisible(final Context context)
			{
				return ListStoreIconButtonCell.this.isCellIconVisible(listStore.findModelWithKey(context.getKey().toString()),
						context.getColumn(), context.getIndex(), context.getKey().toString());
			}
		});
	}

	protected abstract void onClick(T model, final int column, final int row, final String modelKey);

	/**
	 * 
	 * @param cell
	 * @param context
	 * @return whether the cell icon is visible
	 */
	protected boolean isCellIconVisible(final T model, final int column, final int row, final String modelKey)
	{
		return true;
	}

	@Override
	public ImageResource getIcon(final com.google.gwt.cell.client.Cell.Context context)
	{
		return getIcon(listStore.findModelWithKey(context.getKey().toString()), context.getColumn(), context.getIndex(), context
				.getKey().toString());
	}

	@Override
	protected IconAlign getIconAlign(final com.google.gwt.cell.client.Cell.Context context)
	{
		return getIconAlign(listStore.findModelWithKey(context.getKey().toString()), context.getColumn(), context.getIndex(),
				context.getKey().toString());
	}

	@Override
	public String getTooltip(final com.google.gwt.cell.client.Cell.Context context)
	{
		return getTooltip(listStore.findModelWithKey(context.getKey().toString()), context.getColumn(), context.getIndex(), context
				.getKey().toString());
	}

	/**
	 * Returns the icon align.
	 * 
	 * @param model
	 * @param column
	 * @param row
	 * @param modelKey
	 * @return {@link #getIcon()} or use the context
	 */
	public IconAlign getIconAlign(final T model, final int column, final int row, final String modelKey)
	{
		return getIconAlign();
	}

	/**
	 * Get the icon
	 * 
	 * @param model
	 * @param column
	 * @param row
	 * @param modelKey
	 * @return {@link #getIcon()} or use the context
	 */
	public ImageResource getIcon(final T model, final int column, final int row, final String modelKey)
	{
		return getIcon();
	}

	/**
	 * Get the tooltip.
	 * 
	 * @param model
	 * @param column
	 * @param row
	 * @param modelKey
	 * @return {@link #getTooltip()} or use the context
	 */
	public String getTooltip(final T model, final int column, final int row, final String modelKey)
	{
		return getTooltip();
	}
}
